/*
* Author: Felix
* Date: 2007/05/31
*/
if(!(tibco.admin.monitormanage && tibco.admin.monitormanage.logCfg && tibco.admin.monitormanage.logCfg.LogCfg) ){
tibco.admin.Main.loadResource("tibco/admin/monitormanage/logCfg/jss/jss.xml", "tibco.admin", "jss36", "ljss", false);
jsx3.lang.Class.defineClass(
"tibco.admin.monitormanage.logCfg.LogCfg",
jsx3.lang.Object,
[tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceTabInterface],
function (plugin) {
    plugin.instance = null;

    //This value should be from the parent, but for now, it's just a temp value.
    plugin.prototype.serviceId = "1";

    plugin.prototype.isServiceInstanceMode = false;

    plugin.getInstance = function(args) {
        if(!this.instance) {
            this.instance = new tibco.admin.monitormanage.logCfg.LogCfg();
        }
        return this.instance;
    }

    plugin.prototype.getNamespace = function() {
       return "tibco.admin";
    }

    plugin.prototype.getTabText = function() {
    	return tibco.admin.Main.getServer().getDynamicProperty("@@lblSectionTitle_36");
    }

    plugin.prototype.updateComponent = function(serviceId) {
        this.serviceId = serviceId;
    }

    plugin.prototype.setIsServiceInstanceMode = function(isServiceInstanceMode) {
    	this.isServiceInstanceMode = isServiceInstanceMode;
    }

    // get the object managing the component.
    plugin.getHandler = function(obj){
        return obj.getAncestorOfName("lytLogCfg_36").handler;
    }

    plugin.prototype.loadComponent = function(parent) {
        tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/monitormanage/logCfg/ui/logCfg.xml", this, this.componentLoaded);
    }

    plugin.prototype.componentLoaded = function(objEvent) {
        if(objEvent.status == "SUCCESS") {
            this.block = objEvent.obj;
            var me = this;
            this.block.handler = me;
            if(this.serviceId) {
                this.callgetNodeLoggingConfiguration();
            }
        }
        else {
            if(objEvent.exception)
            {
                tibco.admin.Main.logException(objEvent.exception, objEvent.message);
            }
            else {
                jsx3.log(objEvent.message);
            }
        }
    }

    /*
    * Event handler for SCL button
    */
    plugin.prototype.btnSCL_onclick = function(){
        this.logCfgDlg = tibco.admin.Main.loadUIResource(tibco.admin.Main.getServer().getBodyBlock(), "tibco.admin","tibco/admin/monitormanage/logCfg/ui/logCfgDlg.xml");
        this.logCfgDlg.handler = this;
        this.callgetComponentsInNode();
    }

    /*
    * Event handler for edit button
    */
    plugin.prototype.btnEdit_onclick = function(){
        this.toggleMode();
    }

    /*
    * Event handler for save button
    */
    plugin.prototype.btnApply_onclick = function(){
        this.callupdateNodeLoggingConfiguration();
    }

    /*
    * Event handler for cancel button
    */
    plugin.prototype.btnCancel_onclick = function(){
        this.callgetNodeLoggingConfiguration();
        this.toggleMode();
    }

    plugin.prototype.btnCancel_DLG_onclick = function(){
        var objCDFDocument = this.logCfgDlg.getDescendantOfName("mtxService_36").getXML();
        var list = objCDFDocument.getChildNodes();
        var iter = list.iterator();
        while (iter.hasNext()) {
            try {
                var obj = iter.next();
                obj.setAttribute("checked",0);
            }catch(ex) {
                tibco.admin.Main.logException(ex);
            }
        }
        this.logCfgDlg.getDescendantOfName("mtxService_36").repaintData();
    }

    plugin.prototype.btnSave_DLG_onclick = function(){
        //Remove the old containers
        var objCDFLogCfg = this.block.getDescendantOfName("mtxLogCfg_36").getXML();
        var firstChild = objCDFLogCfg.getFirstChild();
        objCDFLogCfg.removeChildren();
        objCDFLogCfg.appendChild(firstChild);

        //Add containers to log list
        var objCDFDocument = this.logCfgDlg.getDescendantOfName("mtxService_36").getXML();
        var list = objCDFDocument.selectNodes("//record[@checked=1]");
        var iter = list.iterator();
        while (iter.hasNext()) {
            try {
                var obj = iter.next();
                objCDFLogCfg.appendChild(obj);
                }catch(ex) {
                    tibco.admin.Main.logException(ex);
                }
        }
        this.logCfgDlg.doClose();
        this.block.getDescendantOfName("mtxLogCfg_36").repaintData();
        this.btnEdit_onclick();
    }

    plugin.prototype.mtx_onchange = function() {
        var objCDFDocument = this.block.getDescendantOfName("mtxLogCfg_36").getXML();
        var objRec = objCDFDocument.selectSingleNode("//record[@jsxid='" + id + "']");
        if(val=="File") {
            objRec.setAttribute("File", "[amx_home]/../data/[environment_name].[node_name]/logs/[environment_name]_[node_name].log");
        }
        if(val=="Web Service" || val=="Console") {
            objRec.setAttribute("File","");
            objRec.setAttribute("MaxFileSize","");
            objRec.setAttribute("MaxBackupIndex","");
        }
        this.block.getDescendantOfName("mtxLogCfg_36").repaintData();
    }

    plugin.prototype.mtx_isEditable = function(id,col) {
        var objCDFDocument = this.block.getDescendantOfName("mtxLogCfg_36").getXML();
        var objRec = objCDFDocument.selectSingleNode("//record[@jsxid='" + id + "']");
        var objAppender = objRec.getAttribute("appender");
        if ((col.getName()=="mcFile_36" || col.getName()=="mcMFS_36" || col.getName()=="mcMBI_36") && (objAppender!="File")) {
            var isFile = false;
        } else {
            var isFile = true;
        }
        return (this.isInEditMode() && isFile);
    }

    plugin.prototype.toggleMode = function() {
        if(this.isInEditMode()) {
            this.endEditMode();
        }
        else {
            this.beginEditMode();
        }
        var applycancel = this.isInEditMode()?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE;
        var edit = this.isInEditMode()?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK;
        this.block.getDescendantOfName("btnApply_36").setDisplay(applycancel, true);
        this.block.getDescendantOfName("btnCancel_36").setDisplay(applycancel, true);
        this.block.getDescendantOfName("btnEdit_36").setDisplay(edit, true);
    }

    /**
    * WSDL Operations
    */

    //call this method to begin the service call (eg.service.callgetNodeLoggingConfiguration();)
    plugin.prototype.callgetNodeLoggingConfiguration = function() {
        var objService = tibco.admin.Main.loadService("tibco.admin",
        "tibco/admin/monitormanage/logCfg/rules/getNodeLoggingConfiguration.xml",
        "getNodeLoggingConfiguration",
        "getNodeLoggingConfiguration");

        if(tibco.admin.Main.getServer().getEnv("MODE")) {
            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
        }

        //subscribe and call
        objService.EntityID = this.nodeId;
        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetNodeLoggingConfigurationSuccess);
        objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.ongetNodeLoggingConfigurationError);
        objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.ongetNodeLoggingConfigurationInvalid);
        objService.setInboundURL("tibco/admin/monitormanage/logCfg/docs/getNodeLoggingConfiguration.xml");
        objService.doCall();
    };

    plugin.prototype.ongetNodeLoggingConfigurationSuccess = function(objEvent) {
        var objCDFDocument = this.block.getDescendantOfName("mtxLogCfg_36").getXML();
        var list = objCDFDocument.getChildNodes(true);
        var iter = list.iterator();
        while (iter.hasNext()) {
            try {
                var objRec = iter.next();

                if (objRec.getAttribute("File") != "") {
                    objRec.setAttribute("appender","File");
                }
                if (objRec.getAttribute("webServiceAppender") != "") {
                    objRec.setAttribute("appender","Web Service");
                }
                if (objRec.getAttribute("consoleAppender") != "") {
                    objRec.setAttribute("appender","Console");
                }

            }catch(ex) {
                tibco.admin.Main.logException(ex);
            }
        }
        this.block.getDescendantOfName("mtxLogCfg_36").repaintData();
    };

    plugin.prototype.ongetNodeLoggingConfigurationError = function(objEvent) {
    };

    plugin.prototype.ongetNodeLoggingConfigurationInvalid = function(objEvent) {
    };

    ////////////////////

    //call this method to begin the service call (eg.service.callupdateNodeLoggingConfiguration();)
    plugin.prototype.callupdateNodeLoggingConfiguration = function() {
        var objService = tibco.admin.Main.loadService("tibco.admin",
        "tibco/admin/monitormanage/logCfg/rules/updateNodeLoggingConfiguration.xml",
        "updateNodeLoggingConfiguration",
        "updateNodeLoggingConfiguration");

        if(tibco.admin.Main.getServer().getEnv("MODE")) {
            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
        }
        //subscribe and call
        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onupdateNodeLoggingConfigurationSuccess);
        objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onupdateNodeLoggingConfigurationError);
        objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onupdateNodeLoggingConfigurationInvalid);
        objService.setInboundURL("tibco/admin/monitormanage/logCfg/docs/getNodeLoggingConfiguration.xml");
        objService.doCall();
    };

    plugin.prototype.onupdateNodeLoggingConfigurationSuccess = function(objEvent) {
        this.toggleMode();
        this.callgetNodeLoggingConfiguration();
    };

    plugin.prototype.onupdateNodeLoggingConfigurationError = function(objEvent) {
    };

    plugin.prototype.onupdateNodeLoggingConfigurationInvalid = function(objEvent) {
    };

    /////////////////////

    //call this method to begin the service call (eg.service.callgetComponentsInNode();)
    plugin.prototype.callgetComponentsInNode = function() {
        var objService = tibco.admin.Main.loadService("tibco.admin",
        "tibco/admin/monitormanage/logCfg/rules/getComponentsInNode.xml",
        "getComponentsInNode",
        "getComponentsInNode");
        //subscribe and call
        if(tibco.admin.Main.getServer().getEnv("MODE")) {
            objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
        }
        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetComponentsInNodeSuccess);
        objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.ongetComponentsInNodeError);
        objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.ongetComponentsInNodeInvalid);
        objService.setInboundURL("tibco/admin/monitormanage/logCfg/docs/getComponentsInNode.xml");
        objService.doCall();
    };

    plugin.prototype.ongetComponentsInNodeSuccess = function(objEvent) {
        this.logCfgDlg.getDescendantOfName("mtxService_36").repaintData();
    };

    plugin.prototype.ongetComponentsInNodeError = function(objEvent) {
    };

    plugin.prototype.ongetComponentsInNodeInvalid = function(objEvent) {
    };

});
}